﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using AutoMapper;
using dd.Tasks.Dtos;
using Microsoft.EntityFrameworkCore;

namespace dd.Tasks
{
	public class TaskAppService : ddAppServiceBase, ITaskAppService
	{
		private readonly IRepository<Core.Tasks.Task> _taskRepository;

		public TaskAppService(IRepository<Core.Tasks.Task> taskRepository)
		{
			_taskRepository = taskRepository;
		}

		public async Task<ListResultDto<TaskListDto>> GetAll(GetAllTasksInput input)
		{
			var tasks = await _taskRepository
				.GetAll()
				.WhereIf(input.State.HasValue, t => t.State == input.State.Value)
				.OrderByDescending(t => t.CreationTime)
				.ToListAsync();

			return new ListResultDto<TaskListDto>(
				ObjectMapper.Map<List<TaskListDto>>(tasks)
			);
		}
	}
}
